package com.app.motion.skindemo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.security.Permission;

import cn.feng.skin.manager.base.BaseActivity;
import cn.feng.skin.manager.listener.ILoaderListener;
import cn.feng.skin.manager.loader.SkinManager;

public class MainActivity extends BaseActivity {

    private String[] mDatas = null;
    private LayoutInflater mLayoutInflater = null;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLayoutInflater = getLayoutInflater();
        setContentView(R.layout.activity_main);
        requestStoragePerssion(this);
        initDatas();
        mListView = this.findViewById(R.id.listview);
        mListView.setAdapter(new SkinAdapter());
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (id == 1) {
                    if (SkinManager.getInstance().isExternalSkin()) {
                        SkinManager.getInstance().restoreDefaultTheme();
                        return;
                    }
                    String skin_path = Environment.getExternalStorageDirectory() + File.separator + "dark.skin";
                    SkinManager.getInstance().load(skin_path, new ILoaderListener() {
                        @Override
                        public void onStart() {
                            Toast.makeText(MainActivity.this, "onstart", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess() {
                            Toast.makeText(MainActivity.this, "change Theme success", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailed() {
                            Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_SHORT).show();

                        }
                    });
                }
            }
        });

    }

    private void requestStoragePerssion(Context context) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x001);
        }
    }

    private void initDatas() {
        mDatas = new String[20];
        for (int i = 0; i < 20; i++) {
            mDatas[i] = ("number:" + i).toUpperCase();
        }
        mDatas[1] = "切换主题";
    }

    class SkinAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mDatas.length;
        }

        @Override
        public Object getItem(int position) {
            return mDatas[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = mLayoutInflater.inflate(R.layout.layout_item, null);
            view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            TextView tv_show = ((ViewGroup) view).findViewById(R.id.tv_item);
            tv_show.setText(mDatas[position]);
            return view;
        }
    }
}
